<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="info" icon="el-icon-refresh" @click="refresh"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.wx_name" placeholder="昵称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe>

                <el-table-column label="活动名称" prop="name" width="250" align="left"> </el-table-column>
                <el-table-column label="开始时间" prop="stime" width="200" align="center"></el-table-column>
				<el-table-column label="结束时间" prop="etime" width="200" align="center"></el-table-column>
				<el-table-column label="抽奖规则" prop="jifen" width="250" align="center">
                        <template #default="scope">
                              每人可抽{{scope.row.chou_num}}次
                        </template>
                </el-table-column>
				<el-table-column label="状态" prop="status" width="110" align="center">
				        <template #default="scope">
				              <el-tag v-if="scope.row.status == 1" type="success">开启</el-tag>
							  <el-tag v-if="scope.row.status == 0" type="error">关闭</el-tag>
				        </template>
				</el-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="350">
					<template #default="scope">
						<el-button-group>

							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button text type="primary" size="small" @click="see(scope.row, scope.$index)">添加奖品</el-button>
							<el-button text type="primary" size="small" @click="prizelist(scope.row, scope.$index)">奖品管理</el-button>
						</el-button-group>
					</template>
				</el-table-column>


			</scTable>
		</el-main>

		<savecat-dialog v-if="dialog.save" ref="savecatDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></savecat-dialog>

		<el-dialog v-model="show" title="添加奖品" width="600" :before-close="handleClose">
				<div>
					<el-form :model="form" label-width="120" style="max-width: 600px;margin-top: 20px;">
						<el-form-item label="奖品类型">
							<el-radio-group v-model="prize.type">
									<el-radio :label="1">商品</el-radio>
									<el-radio :label="2">积分</el-radio>
									<el-radio :label="3">优惠券</el-radio>
									<el-radio :label="10">谢谢参与</el-radio>
							</el-radio-group>
						</el-form-item>

						<el-form-item label="奖品名称">
						   <el-input v-model="prize.name" type="text" placeholder="名称尽量简单"/>
						</el-form-item>
						<el-form-item label="积分" v-if="prize.type == 2">
						   <el-input v-model="prize.prize" type="text" placeholder=""/>
						</el-form-item>
						<el-form-item label="选择商品" v-if="prize.type == 1">
								<el-select v-model="prize.prize"  filterable style="width: 100%" clearable @visible-change="getpro">
									<el-option v-for="item in pro" :key="item.cgd" :label="item.title" :value="item.gid"/>
								</el-select>
						</el-form-item>
						<el-form-item label="选择优惠券" v-if="prize.type == 3">
								<el-select v-model="prize.prize"  filterable style="width: 100%" clearable @visible-change="getquan">
									<el-option v-for="item in quan" :key="item.id" :label="item.title" :value="item.id"/>
								</el-select>
						</el-form-item>
						<el-form-item label="库存">
						   <el-input v-model="prize.stock" type="number" placeholder=""/>
						</el-form-item>
						<el-form-item label="中奖概率">
						   <el-input v-model="prize.probability" type="number" placeholder=""/>
						</el-form-item>
						<el-form-item label="排序">
						   <el-input v-model="prize.sort" type="number" placeholder="数字越大 越靠前"/>
						</el-form-item>
						<el-button type="primary" @click="onSubmitPrize" size="large">保 存</el-button>
					</el-form>
				</div>
		</el-dialog>


		<el-dialog v-model="show2" title="奖品管理" width="65%" :before-close="handleClose">
				<div>
					  <el-table :data="allprize" border style="width: 100%">

					    <el-table-column prop="name" label="奖品"/>
						<el-table-column prop="type" label="类型" width="100" align="center">
							<template #default="scope">
								 <el-tag v-if="scope.row.type==1" type="primay">商品</el-tag>
								 <el-tag v-if="scope.row.type==2" type="error">积分</el-tag>
								 <el-tag v-if="scope.row.type==3" type="success">优惠券</el-tag>
								 <el-tag v-if="scope.row.type==10" type="info">谢谢参与</el-tag>
							</template>
						</el-table-column>
					    <el-table-column prop="stock" label="库存" width="100" align="center"/>
						<el-table-column prop="probability" label="概率" width="100" align="center"/>
						<el-table-column prop="sort" label="排序" width="100" align="center"/>
						<el-table-column prop="sort" label="中奖数量/剩余" width="110" align="center"/>
						<el-table-column prop="sort" label="操作" align="center" width="180" >
								<template #default="scope">

										<el-popconfirm title="确定删除吗？" @confirm="delprize(scope.row, scope.$index)">
											<template #reference>
												<el-button text type="primary" size="small">删除</el-button>
											</template>
										</el-popconfirm>
								</template>
						</el-table-column>
					  </el-table>
				</div>
		</el-dialog>




	</el-container>
</template>

<script>
	import savecatDialog from './turntable_save'
	export default {
		name: 'mk_jifen',
		components: {
			savecatDialog
		},
		data() {
			return {
				game_id:'',
				allprize:[],
				prize:{},
				pro:[],
				quan:[],
				show:false,
				show2:false,
				this_id:'',
				dialog: {
					save: false
				},
				apiObj: this.$API.system.mk.turntable,
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		methods: {
			async getpro(){
				if(this.pro.length>0) return
				var res = await this.$API.system.universal.list.get({nopage:1},'/goods/goods');
				if(res.code == 200){
					 this.pro = res.data
				}
			},
			async getquan(){
				if(this.quan.length>0) return
				var res = await this.$API.system.universal.list.get({nopage:1},'/mk/coupon');
				if(res.code == 200){
					 this.quan = res.data
				}
			},
			async onSubmitPrize(){
				var res = await this.$API.system.universal.handle.post(this.prize,'/mk/game_prize_save');
				if(res.code == 200){
					this.prize.name = ''
					this.prize.type = ''
					this.prize.probability = ''
					this.prize.prize = ''
					this.$message.success("保存成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async prizelist(row){
				this.show2 = true
				this.game_id = row.id
				var res = await this.$API.system.universal.list.get({nopage:1,'game_id':row.id,'noneed':1},'/mk/game_prize');
				if(res.code == 200){
					  this.allprize = res.data
				}
			},
			async delprize(row){
				var reqData = {id: row.id,'game_id':this.game_id}
				var res = await this.$API.system.universal.handle.post(reqData,'/mk/game_prize_del');
				if(res.code == 200){
					this.prizelist({'id':this.game_id})
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			see(row){
				this.prize.game_id = row.id
				this.show = true
			},
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.savecatDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.savecatDialog.open('edit').setData(row)
				})
			},
			//删除
			async table_del(row){
				var reqData = {id: row.id,'act':'del'}
				var res = await this.$API.system.universal.handle.post(reqData,'/mk/turntable_del');
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
					type: 'warning'
				}).then(() => {
					var arr = this.selection
					var ob = []
					for(var i=0;i<arr.length;i++){
						  ob.push(arr[i].cid)
					}
					this.table_del({'cid':ob.join(",")})
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//表格内开关
			async changeSwitch(val, row){
				var res = await this.$API.system.universal.handle.post({'id':row.id,'status':val},'/mk/jifen_save')
				if(res.code == 200){
					this.visible = false;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
            refresh(){
                this.$refs.table.reload()
            },
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add' || mode =='edit'){
					this.$refs.table.reload()
				}
			}
		}
	}
</script>

<style>
.red{
    font-size: 15px;
    color:#ff3333
}
.feng{
    width:40px;
    height:40px;
	border-radius: 800px

}
</style>

